<?php
include_once libfile('function/square');
if($_GET['action']=='create'){
	if($_G['uid']){
		if($_GET['type']=='short'){
			//发布类型为短文章
			$ret=array();
			if($_GET['message']){
				$thread=array();
				$thread['typeid']=1;
				$thread['fid']=7;
				$tid=C::t('city_thread')->insert($thread,true);
				if($tid){
					$data['typeid']=1;
					$data['tid']=$tid;
					$data['author']=$_G['username'];
					$data['authorid']=$_G['uid'];
					$data['dateline']=time();
					$data['message']=$_GET['message'];
					//处理#话题
					$tagmsg=find_first_tag_from_msg($_GET['message']);//返回数组,并且已经把话题写入tag表
					if(!empty($tagmsg)&&$tagmsg){
						$data['tagid']=$tagmsg['tagid'];
						$data['tagname']=$tagmsg['tagname'];
					}else{
						$data['tagid']=$tag['id']?$tag['id']:'0';
						$data['tagname']=$tag['name']?$tag['name']:'';
					}
					//TODO:处理@用户
					$data['svalues']='';
					//$data['client']=$_GET['client']?checkclient($_GET['client']):'';
					$sid=C::t('city_square')->insert($data,true);
					C::t('user_count')->update_squares_by_uid($_G['uid']);
					if($sid){
						$ret['status']=1;
						$subsquare=C::t('city_square')->fetch_by_sid($sid);
						$ret['data']=$subsquare;
						$ret['sid']=$sid;
					}
				}else{
					$ret['status']=-3;
				}
					
			}else{
				$ret['status']=-2;
			}
			echo json_encode($ret);
			exit();
		}elseif($_GET['type']=='long'){
			//TODO:发布类型为长文章
			$title=$_GET['title'];
		}elseif($_GET['type']=='image'){//每上传一张图片都产生一个create与type,并且对资源表进行写入
			//TODO:发布类型为图片
		}elseif($_GET['type']=='audio'){
			//发布类型为音频:暂不支持音频发布
		}elseif($_GET['type']=='video'){
			//发布类型为视频
			$ret=array();
			if(!empty($_GET['message'])){
				if(!empty($_GET['imgurl'])&&!empty($_GET['flv'])&&!empty($_GET['url'])){
					$thread=array();
					$thread['typeid']=1;
					$thread['fid']=7;
					$tid=C::t('city_thread')->insert($thread,true);
					$data=array();
					if($tid){
						$data['typeid']=5;
						$data['tid']=$tid;
						$data['author']=$_G['username'];
						$data['authorid']=$_G['uid'];
						$data['dateline']=time();
						$data['message']=$_GET['message'];
						//处理#话题
						$tagmsg=find_first_tag_from_msg($_GET['message']);//返回数组,并且已经把话题写入tag表
						if(!empty($tagmsg)&&$tagmsg){
							$data['tagid']=$tagmsg['tagid'];
							$data['tagname']=$tagmsg['tagname'];
						}else{
							$data['tagid']=$tag['id']?$tag['id']:'0';
							$data['tagname']=$tag['name']?$tag['name']:'';
						}
						//TODO:处理@用户
						$data['svalues']=array();
						$data['svalues']['imgurl']=urlencode($_GET['imgurl']);
						$data['svalues']['flv']=urlencode($_GET['flv']);
						$data['svalues']['url']=urlencode($_GET['url']);
						$data['svalues']=serialize($data['svalues']);
						//$data['client']=$_GET['client']?checkclient($_GET['client']):'';
						$sid=C::t('city_square')->insert($data,true);
						C::t('user_count')->update_squares_by_uid($_G['uid']);
						if($sid){
							$ret['status']=1;
							$subsquare=C::t('city_square')->fetch_by_sid($sid);
							$ret['data']=$subsquare;
							$ret['sid']=$sid;
						}
					}
				}else{
					$ret['status']=-2;
				}
			}else{
				$ret['status']=-3;
			}
			echo json_encode($ret);
			exit();
		}elseif($_GET['type']=='loss'){
			$photos=$_FILES['photos'];
			foreach ($photos as $photo){
				if($photo['name'] !=''){
					if($photo['error'] > 0){
						if($photo['type'] =='image/jpeg' || $photo['type'] =='image/pjpeg' || $photo['type'] =='image/gif'){
							if($photo['size']<=20480){
								$path=date('Ym');//这里设置文件夹的名字,未定
								$newname=date('Ymdhis').strstr($photo['type'],".");
								move_uploaded_file($photo['tmp_name'] ,"uploads/".$path);//将上传图片放入年份月份命名的文件夹
								rename("uploads/".$path."/".$photo['name'],$newname);//修改名字为日期
								$data=array();
								$data['tid']="";//这里的插入主题id有时间上的问题
								$data['dateline']=time();
								$data['filename']=$newname;
								$data['mimetype']=strstr($photo['type'],".");
								$data['filesize']=$photo['size'];
								$data['readperm']=0;
								$data['price']=0;
								$data['path']="uploads/".$path."/";//文件夹名
								$data['downloads']=0;
								C::t('common_attachment')->insert($data);
							}else{
								//处理图片过大的错误
							}
						}else{
							//处理不是图片文件的错误
						}
					}else{
						$photo['error'];//处理错误
					}
				}else{
					//处理上传文件无的错误
				}
			}
			//TODO:发布类型为捡到物品
			$type=6;
		}elseif($_GET['type']=='getback'){
			//这里应该会得到几张失物的图片,可以按照处理上传图片的代码进行处理//前端传回来的是photos[]数组
			$photos=$_FILES['photos'];
			foreach ($photos as $photo){
				if($photo['name'] !=''){
					if($photo['error'] > 0){
						if($photo['type'] =='image/jpeg' || $photo['type'] =='image/pjpeg' || $photo['type'] =='image/gif'){
							if($photo['size']<=20480){
								$path=date('Ym');//这里设置文件夹的名字,未定
								$newname=date('Ymdhis').strstr($photo['type'],".");
								move_uploaded_file($photo['tmp_name'] ,"uploads/".$path);//将上传图片放入年份月份命名的文件夹
								rename("uploads/".$path."/".$photo['name'],$newname);//修改名字为日期
								$data=array();
								$data['tid']="";//这里的插入主题id有时间上的问题
								$data['dateline']=time();
								$data['filename']=$newname;
								$data['mimetype']=strstr($photo['type'],".");
								$data['filesize']=$photo['size'];
								$data['readperm']=0;
								$data['price']=0;
								$data['path']="uploads/".$path."/";//文件夹名
								$data['downloads']=0;
								C::t('common_attachment')->insert($data);
							}else{
								//处理图片过大的错误
							}
						}else{
							//处理不是图片文件的错误
						}
					}else{
						$photo['error'];//处理错误
					}
				}else{
					//处理上传文件无的错误
				}
			}
			//TODO:发布类型为丢失物品
			$type=7;
		}else{
			exit('notvalid type');
		}
		$massage=$_GET['massage'];//这里处理massage
		if(isset($title)){//这里是处理长文章的标题
			$massage=substr_replace($massage,strlen($title)." ".$title." ",0,0);
		}
		
		$mag=array();//这里先处理发布非长文章的信息
		$tag=find_first_tag_from_msg($massage);
		if(is_array($tag)){
			$msg['tagname']=$tag['tagname'];
			$msg['tagid']=$tag['tagid'];
		}
			
		$msg['typeid']=$type;
		$msg['client'] = $_G['clientip'];
		$msg['author'] = $_G['username'];
		$msg['authorid']=$_G['uid'];
		$msg['sid']="";
		$msg['tid']=$_G[''];
		$msg['dateline']=time();
		$msg['massage']=$massage;
		C::t('city_square')->insert($msg);
		if($atlist=getAtlist($_GET['message'])){
			//处理的方法为    extract_call_from_massage在function_square.php
			//TODO:处理@用户
		}
		$sharerenren=$_GET['renren']?'renren':'';
		$shareweibo=$_GET['weibo']?'weibo':'';
		$shareqq=$_GET['qq']?'qq':'';
		
	}else{
		dheader('Location:index.php?mod=user&action=login');
	}
}elseif($_GET['action']=='del'){
	$tid=intval($_GET['tid']);
	if($tid){
		if($_G['level']==127){
			C::t('city_square')->update_by_tid(array('isdel'=>1));
			$out=outJson(array('status'=>'ok'));
			exit();
		}else{
			$twrtterres=C::t('city_thread')->fetch_first($tid);
			if($twrtterres['authorid']==$_G['uid']){
				C::t('city_square')->update_by_tid(array('isdel'=>1));
				$out=outJson(array('status'=>'ok'));
				exit();
			}else{
				$out=outJson(array('status'=>'noperm'));
				exit();
			}
		}
	}else{
		$out=outJson(array('status'=>'notid'));
		exit();
	}
}elseif($_GET['action']=='sharecheck'){//检测是否绑定分享网站
	$tid=intval($_GET['tid']);
	if($tid){//分享
		switch($_GET['type']){
			case 'renren':{
				//TODO:人人分享
				break;
			}
			case 'weibo':{
				//TODO:微博分享
				break;
			}
			case 'qq':{
				//TODO:QQ分享
				break;
			}
			default:$out=outJson(array('status'=>'unknowsharetype'));
		}
	}
	exit();
}elseif($_GET['action']=='nice'){
	//赞
	$res=array();
	$type=in_array($_GET['type'],array('tid','rid'))?$_GET['type']:'';//赞类型
	if($type){
		$id=$_GET['id']?intval($_GET['id']):'';//所赞的tid或rid值
		if($id){
			if($type=='tid'){
				C::t('city_square')->update_nice_by_tid($id);
			}elseif($type=='rid'){
				C::t('city_reply')->update_nice_by_rid($id);
			}
			$res['status']='1';
		}else{
			$res['status']='-1';
		}
	}else{
		$res['status']='-2';
	}
	echo json_encode($res);
	exit();
}elseif($_GET['action']=='getreply'){
	$tid=$_GET['tid']?intval($_GET['tid']):'';//tid
	$end=$_GET['end']?intval($_GET['end']):'0';//已经读取的最大rid值 $limit 为要读取的记录数
	$limit=$end?($_G['setting']['replylimit']?intval($_G['setting']['replylimit']):3):($_G['setting']['replynextlimit']?intval($_G['setting']['replynextlimit']):10);
	if($tid){//获取回复
		$reply=C::t('city_reply')->fetch_all_by_tid($tid,$end,$limit);
		list($reply,$maxrid)=processreply($reply);
		$replynext=C::t('city_reply')->count_by_tid_rid($tid,$maxrid);
	}
	include template('square_reply');
	exit();
}elseif($_GET['action']=='refreshData'){
	//TODO:定时刷新消息
	$tid=$_GET['tid']?intval($_GET['tid']):'';
	$res=array();
	if($tid){
		$data=array();
		$data=C::t('city_square')->fetch_all_by_tid_isdel($tid,0,0,10,'<');
		$data=processsquare($data);
		if(!empty($data)){
			$res['data']=$data;
			$res['status']=1;//正常返回
		}else{
			$res['status']=0;//无数据
		}
	}else{
		$res['status']=-1;//参数不正确
	}
	echo json_encode($res);
	exit();
}elseif($_GET['action']=='reply'){
	//回复
	$tid=$_GET['tid']?intval($_GET['tid']):'';
	$pid=$_GET['pid']?intval($_GET['pid']):'';
	$ret=array();
	if($tid && $_GET['message']){
		$thread=C::t('city_square')->fetch_by_tid($tid);
		if(!empty($thread)){
			$data=array();
			$data['tid']=$tid;
			$data['tuid']=$thread['authorid'];
			$data['ruid']=$_G['uid'];
			$data['typeid']=1;
			$data['message']=$_GET['message'];
			$data['dateline']=time();
			$rid=C::t('city_reply')->insert($data,true);
			C::t('city_square')->update_reply_by_tid($tid);
			$ret['reply']=getreply($rid);
			$ret['status']=1;
			//处理@用户
		}else{
			$ret['status']=-2;
		}
	}else{
		$ret['status']=-1;
	}
	echo json_encode($ret);
	exit();
}elseif($_GET['action']=='getsquare'){//单独请求广场内容
	if($_GET['stype']=='hot'){
		//TODO:热门信息
		//$hotresult=C::t('common_tag')->get_hot_tag();
		//$random=random_number();
        $hot=array();
		foreach($random as $v){
			$hot[]=$hotresult[$v];//$hot为存放了最热的30条tag中的随机十条
		}
	}elseif($_GET['stype']=='my'){
		//TODO:订阅信息
		$start=($_G['page']-1)*10;
		$limit=10;
		$square=C::t('city_square')->fetch_all_by_authorid_isdel($_G['uid'],0,$start,$limit);
		if(!empty($square)){
			$square=processsquare($square);
		}
	}elseif($_GET['stype']=='new'){
		//最新信息
		$start=($_G['page']-1)*10;
		$limit=10;
		$square=C::t('city_square')->fetch_all_by_tid_isdel(0,0,$start,$limit);
		if(!empty($square)){
			$square=processsquare($square);
		}
	}else{
		//TODO:默认消息
		//$mosthot=C::t('city_square')->get_most_hot_massage(3);//三条最热门
		$start=($_G['page']-1)*10;
		$limit=10;
		$square=C::t('city_square')->fetch_all_by_tid_isdel(0,0,$start,$limit);
		if(!empty($square)){
			$square=processsquare($square);
		}
	}
	include template('square_main');
	exit();
}elseif($_GET['action']=='refreshData'){//定时刷新消息
	$tid=$_GET['tid']?intval($_GET['tid']):'';
	$res=array();
	if($tid){
		$data=array();
		$data=C::t('city_square')->fetch_all_by_tid_isdel($tid,0,$start,$limit,'>');
		$data=processsquare($data);
		if(!empty($data)){
			$res['data']=$data;
			$res['status']='1';
		}else{
			$res['status']='0';
		}
	}else{
		$res['status']='-1';
	}
	echo json_encode($res);
	exit();
}
/*
 * 显示失物信息已经移动位置
if($_GET['action']=='display_lose'){//这个action是我自己加的
	$squarelose=C::t('city_square')->fetch_all_by_field(array('typeid'=>'7'));//这里返回二维数组
	$loses=array();
	foreach ($squarelose as $reslose){
		$tid=$reslose['tid'];//得到所有的丢失物品的tid
		$losephotos=C::t('common_attachment')->fetch_all_by_field(array('tid'=>$tid));//传回来二维数组,是一些图片的信息
		//$loses[]=;//这里要把$reslose这个失物贴的一位数组与$losephotos这个贴对应的图片的二维数组相连
	}
}
*/

//默认：显示广场主内容

//显示页面其他信息
$square=C::t('city_square')->fetch_all_by_tid_isdel(0,0,0,10);
if(!empty($square)){
	$square=processsquare($square);
}
$smiletype=array();
$smilelist=unserialize($_G['setting']['smilelist']);
$weather=getweather();
$headimg=avatar($_G['uid'],'middle',false,false,'../ucenter');
$_G['user']['check']=C::t('user_check')->fetch_first_by_uid($_G['uid']);//签到信息
$taglist=C::t('common_tag')->fetch_all(0,10);
$userstat=C::t('user_count')->fetch_first_by_uid($_G['uid']);
//echo parseflv('http://v.youku.com/v_show/id_XNjk4MTQxMTI4.html',400,300);
//exit();
include template('square');


?>
